/*!
    \file    main.c
    \brief   ADC_software_trigger_regular_channel_polling

   \version 2024-12-20, V3.0.1, firmware for GD32F30x
*/

/*
    Copyright (c) 2024, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "detect_voltage.h"

static void detcet_init(void);
static void rcu_config(void);
static void gpio_config(void);
static void adc_config(void);
static void detect_adc_stop(void);

static uint16_t adc_channel_sample(uint8_t channel);
static uint16_t get_voltage_result(enum DETECT_INPUT DEVICE);

static struct detect_adc_func detect_voltage;
/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
struct detect_adc_func *detect_driver_init(void)
{
    struct detect_adc_func *adc_func = &detect_voltage;
    /* clocks configuration */
    adc_func->detect_adc_init = detcet_init;
    adc_func->detect_result = get_voltage_result;
    adc_func->detect_stop = detect_adc_stop;
    return adc_func;
}
static uint16_t get_voltage_result(enum DETECT_INPUT DEVICE)
{
    uint16_t voltage;
    switch (DEVICE)
    {
    case EMS_ADC:
        /* code */
        voltage = adc_channel_sample(ADC_CHANNEL_1);
        break;
    case VBAT_ADC:
        /* code */
        voltage = adc_channel_sample(ADC_CHANNEL_2);
        break;
    case PUMP_ADC:
        /* code */
        voltage = adc_channel_sample(ADC_CHANNEL_3);
        break;

    default:
        break;
    }
    return voltage;
}
static void detcet_init(void)
{
    rcu_config();
    gpio_config();
    adc_config();
    return;
}
/*!
    \brief      configure the different system clocks
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void rcu_config(void)
{
    /* enable GPIOA clock */
    rcu_periph_clock_enable(DETECT_GPIO_CLK);
    /* enable ADC clock */
    rcu_periph_clock_enable(RCU_ADC0);
    /* config ADC clock */
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6);
    return;
}

/*!
    \brief      configure the GPIO peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void gpio_config(void)
{
    /* config the GPIO as analog mode */
    gpio_init(EMS_ADC_GPIO_PORT, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, EMS_ADC_PIN);
    gpio_init(VBAT_ADC_GPIO_PORT, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, VBAT_ADC_PIN);
    gpio_init(PUMP_ADC_GPIO_PORT, GPIO_MODE_AIN, GPIO_OSPEED_10MHZ, PUMP_ADC_PIN);
    return;
}

/*!
    \brief      configure the ADC peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void adc_config(void)
{
    /* ADC mode config */
    adc_mode_config(ADC_MODE_FREE);
    /* ADC data alignment config */
    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
    /* ADC channel length config */
    adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1U);

    /* ADC trigger config */
    adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
    /* ADC external trigger config */
    adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);

    /* enable ADC interface */
    adc_enable(ADC0);
    delay_1ms(1U);
    /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC0);

    return;
}

static void detect_adc_stop(void)
{
    /* enable ADC interface */
    adc_disable(ADC0);
    return;
}
/*!
    \brief      ADC channel sample
    \param[in]  none
    \param[out] none
    \retval     none
*/
static uint16_t adc_channel_sample(uint8_t channel)
{
    /* ADC regular channel config */
    adc_regular_channel_config(ADC0, 0U, channel, ADC_SAMPLETIME_7POINT5);
    /* ADC software trigger enable */
    adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);

    /* wait the end of conversion flag */
    while (!adc_flag_get(ADC0, ADC_FLAG_EOC))
        ;
    /* clear the end of conversion flag */
    adc_flag_clear(ADC0, ADC_FLAG_EOC);
    /* return regular channel sample value */
    return (adc_regular_data_read(ADC0));
}
